# 高阶函数

import time, functools

def func(x):
    """
    :param x: 输入参数
    :return: 返回参数的平方
    """
    return x * x
r = map(func, [1, 2, 3, 4, 5])
print(list(r))

# 函数作为返回值
def lazy_sum(*args):
    """
    :param args: 输入参数
    :return: 返回参数
    """
    def sum():
        ax = 0
        for n in args:
            ax = ax + n
        return ax
    return sum
f = lazy_sum(1, 3, 5, 7, 9)
print(f())

def count():
    fs = []
    for i in range(1, 4):
        def f():
             return i*i
        fs.append(f)
    return fs

f1, f2, f3 = count()
print(f1(), f2(), f3())

def inc():
    x = 1
    def fn():
        nonlocal x
        x = x + 1
        return x
    return fn

f = inc()
print(f(), f(), f())



def metric(fn):
    def wrapper(*args, **kw):
        t1 = time.time()
        r = fn(*args, **kw)
        t2 = time.time()
        print('%s executed in %s ms' % (fn.__name__, 1000 * (t2 - t1)))
        return r
    return wrapper

# 测试
@metric
def fast(x, y):
    time.sleep(0.0012)
    return x + y

@metric
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z

f = fast(11, 22)
s = slow(11, 22, 33)
if f != 33:
    print('测试失败!')
elif s != 7986:
    print('测试失败!')
